//Pietras93OS
//Math
//pietras93@gmail.com
//


#include "include/math.h"

double sqrt(double x)
{
        double i = x/2;
        if(x<0) return 0;
        while(fabs(i-(x/i))/i>0.000000000000001) /* (15 DP) ?HOW GET MORE? */
                i=(i+(x/i))/2;
        return i;
}

double fabs(double x)
{
        if(x<0) return -x;
        return x;
}

double hypot(double x, double y)
{
        return sqrt(x*x+y*y);
}

double ceil(double x)
{
        if(x<0) return (int)x;
        return ((int)x)+1;
}

double cbrt(double x)
{
        double i = x/4;
        while(fabs(i-(x/i/i))/i>0.00000000000001) /* (15 DP) ?HOW GET MORE? */
                i=(i+(x/i/i)+i)/3;
        return i;
}

double copysign(double x,double signval)
{
        if(signval<0 && x<0) return  x;
        if(signval<0 && x>0) return -x;
        if(signval>0 && x>0) return  x;
        if(signval>0 && x<0) return -x;
        return x;
}

double fdim(double a, double b)
{
        if(a-b<1) return 0;
        return a-b;
}

double fmax(double a, double b)
{
        if(a>b) return a;
        return b;
}

double floor(double x)
{
        if(x>0) return (int)x;
        return (int)(x-0.9999999999999999);
}

double fmin(double a, double b)
{
        if(a<b) return a;
        return b;
}

double fmod(double a,double b)
{
        return (int)((((a/b)-((int)(a/b)))*b)+0.5);
}

double round(double arg)
{
        return (int)(arg+0.5);
}

double rint(double arg)
{
        return round(arg);
}

double remainder(double a, double b)
{
        return fmod(a,b);
}

int trunc(double arg)
{
        return (int)arg;
}

double nearbyint(double arg)
{
        return round(arg);
}

double exp2(double exp)
{
        int i;
        double value=1;
        for(i=1;i<=exp;i++){
           value=value*2;
        }
        return value;
}

double fma(double a,double b,double c)
{
        return (int)((a*b)+c+0.5);
}

double ldexp(double num, int exp)
{
        return num*exp2(exp);
}
